package com.p2p.backend.service.impl;

import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import jodd.util.StringUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.p2p.backend.dao.P2PRechargeDao;
import com.p2p.backend.model.P2PRecharge;
import com.p2p.backend.service.P2PRechargeService;
import com.p2p.base.constents.InnerAccountConsts;
import com.p2p.base.model.Money;
import com.p2p.base.page.PageVo;
import com.p2p.front.enums.UserCreditsType;
import com.p2p.front.service.UserCreditsService;
import com.p2p.trans.client.facade.AccountTransFacade;
import com.p2p.trans.client.model.TransInfo;
import com.p2p.trans.client.result.AccountTransResult;

@Service("p2PRechargeService")
public class P2PRechargeServiceImpl implements P2PRechargeService {

	private static final Logger logger = LoggerFactory
			.getLogger(P2PRechargeServiceImpl.class);

	@Resource
	private P2PRechargeDao p2PRechargeDao;

	@Resource
	private AccountTransFacade accountTransFacade;
	
	@Resource
	private UserCreditsService userCreditsService;

	public void saveP2PRecharge(P2PRecharge p2pRecharge) {
		p2PRechargeDao.saveP2PRecharge(p2pRecharge);
	}

	public P2PRecharge findP2PRechargeById(Long id) {
		return p2PRechargeDao.findP2PRechargeById(id);
	}

	public void updateP2PRecharge(P2PRecharge p2pRecharge) {
		p2PRechargeDao.updateP2PRecharge(p2pRecharge);
	}



	@Transactional(readOnly=false,propagation=Propagation.REQUIRED)
	public void doPayment(Long id, String ip) {
		P2PRecharge p2pRecharge = findP2PRechargeById(id);
		p2pRecharge.setStatus(P2PRecharge.Status.SUCCESS.getStatus());
		p2pRecharge.setUpTime(Calendar.getInstance().getTimeInMillis());
		p2pRecharge.setOkTime(Calendar.getInstance().getTimeInMillis());
		p2pRecharge.setUpIp(ip);
		p2pRecharge.setAremark("人工");
		updateP2PRecharge(p2pRecharge);

		TransInfo transInfo = new TransInfo();

		// 充值账号
		transInfo.setTransAccountIn(p2pRecharge.getTradeAccount());
		// 总帐总帐账号
		transInfo.setTransAccountOut(InnerAccountConsts.innerTotalAccountNo);
		Money money = p2pRecharge.getMoney();

		//Money m = new Money(money);
		

		// 充值金额
		transInfo.setTransAmount(money);
		// 充值做帐
		// transInfo.setTransOutOrderNo(OrderNo);
		AccountTransResult result = accountTransFacade.doDeposit(transInfo);
		if (!result.isSuccess()) {
			logger.error("accountTransFacade.doDeposit error,code="
					+ result.getResultCode());
		}else{
			
			userCreditsService.addValue(p2pRecharge.getUid(), UserCreditsType.UserCreditsNeedCharge, money.getCent());
			logger.info("UserCreditsNeedCharge update uid="+p2pRecharge.getUid()+",amount = "+money.toString() );
		}

	}

	@Override
	public PageVo listPage(PageVo form) {
		List<Map<String,Object>> list = p2PRechargeDao.listPage(form);
		form.setList(list);
		return form;
	}
	
	public PageVo listPageForResultMap(PageVo form){
		List<P2PRecharge> list = p2PRechargeDao.listPageForResultMap(form);
		form.setList(list);
		return form;
	}
	

	@Override
	public Money sumForCondition(Map<String, Object> parames) {
		Long money = p2PRechargeDao.sumForCondition(parames);
		if(money==null){
			money = new Long(0);
		}
		return new Money(money);
	}

}
